package com.kuizii.demo;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@ComponentScan
@SpringBootApplication
public class DemoStarter {
    static String encryptClassPath = System.getProperty("user.dir")+"\\demo-decrypt\\target\\classes";
    static String securityKey = "__wwW.kuizIi.CoM";


    public static void main(String[] args) {
        DemoClassLoader demoClassLoader = new DemoClassLoader(encryptClassPath, securityKey);
        try {
            String name = "com.kuizii.demo.Demo";

          //  List<String> classes = nameResolver(encryptClassPath);

            Class<?> loadedClass = demoClassLoader.loadClass(name);

            String realArgs[] = new String[]{};
            Object argsArray[] = {realArgs};
            Class mainArgs[] = {(new String[1]).getClass()};

            // Method mainMethod = loadedClass.getMethod("main", mainArgs);
            //  Object o = loadedClass.newInstance();
            //  mainMethod.invoke(null, argsArray);

            for (Method method : loadedClass.getMethods()) {
                if (method.getName().equalsIgnoreCase("main")) {
                    method.invoke(null, argsArray);

                    System.out.println("====================================================================");
                    System.out.println("====================================================================");
                    System.out.println("======>>                                                <<==========");
                    System.out.println("======>>                                                <<==========");
                    System.out.println("======>>                    解密&&启动成功                 <<==========");
                    System.out.println("======>>                                                <<==========");
                    System.out.println("======>>                                                <<==========");
                    System.out.println("====================================================================");
                    System.out.println("====================================================================");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        //SpringApplication.run(com.kuizii.demo.DemoStarter.class, args);

    }

    public static List<String> nameResolver(String classPath) {

        List<String> array = new ArrayList<>();
        getClasses(new File(classPath), array);

        return array;

    }

    private static void getClasses(File file, List<String> files) {

        if (file.isFile() && file.getName().endsWith(".xclass")) {
            files.add(file.getPath().replace(encryptClassPath,"").replace(".xclass","").replace("\\","."));

        } else if (file.isDirectory()) {
            for (File listFile : file.listFiles()) {
                getClasses(listFile, files);
            }
        }
    }
}